
#pragma once

#include "Informations/GlobalInfo.h"
#include "UtilityProps.h"
#include "Runtime.Low/Utils.h"

using namespace System;

//Static class, which contains casting routines between managed and umanaged essences.
class CastingRoutines
{
	public:
		//Casts unmanaged utility properties to managed.
		static ttg_internal::utils::UtilityProps ^castUtilityProps(ttg::utils::UtilityProps *props)
		{
			ttg_internal::utils::UtilityProps ^res 
				= gcnew ttg_internal::utils::UtilityProps(gcnew String(props->getName()));
			res->IsHidden = props->isHidden;
			res->Position = (ttg_internal::utils::UtilityPosition)(int)props->position;
			return res;
		}

		//Cast System::String to std::string.
		static std::string castString(String ^str)
		{
			std::string res(str->Length, '\0');
			for (int i = 0; i < str->Length; i++)
				res[i] = (char)str[i];
			return res;
		}

		//Updates devices in target global info. This method is thread safe.
		static void updateGlobalInfo(ttg_internal::utils::GlobalInfo ^target, ttg::services::DeviceInfo **devices, size_t count)
		{
			target->Lock();
			for (size_t i = 0; i < count; i++)
			{
				//Getting corresponded device.
				ttg_internal::utils::DeviceInfo ^device;
				if ((device = target->GetDeviceInfo(gcnew String(devices[i]->getName()))) == nullptr)
				{
					device = gcnew ttg_internal::utils::DeviceInfo(gcnew String(devices[i]->getName()),
																   gcnew String(devices[i]->getDescription()),
																   gcnew String(devices[i]->getMeasuringUnit()),
																   devices[i]->getMaxValue());
					target->AddDeviceInfo(device);
				}
				//Updating.
				device->Usage = devices[i]->getUsage();
				//Updating subsystems.
				for (size_t j = 0; j < devices[i]->getSubDevices().size(); j++)
				{
					//Getting corresponded subsystem.
					ttg_internal::utils::SubSystemInfo ^ssystem;
					if ((ssystem = device->GetSubSystemInfo(gcnew String
												(devices[i]->getSubDevices()[j]->getName()))) == nullptr)
					{
						ssystem = gcnew ttg_internal::utils::SubSystemInfo(
											gcnew String(devices[i]->getSubDevices()[j]->getName()),
											gcnew String(devices[i]->getSubDevices()[j]->getMeasuringUnit()),
											devices[i]->getSubDevices()[j]->getMaxValue());
						device->AddSubSystem(ssystem);
					}
					//Updating.
					ssystem->Usage = devices[i]->getSubDevices()[j]->getUsage();
				}
			}
			target->UnLock();
		}
};
